package com.umeox.watch.moto.contact.adapter;


import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.umeox.moto.common.clicklistener.OnNoDoubleClickListener;
import com.umeox.moto.watch.themes.skin.loader.ResourceManager;
import com.umeox.moto.watch.themes.widget.RoundedImageView;
import com.umeox.watch.moto.contact.R;
import com.umeox.watch.moto.contact.base.GlideApp;
import com.umeox.watch.moto.contact.model.MergeCallLog;
import com.umeox.watch.moto.contact.utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Rambo 2019-07-05
 * Describe:
 */
public class CallLogAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private List<MergeCallLog> mDatas = new ArrayList<>();
    private Context mContext;
    private final LayoutInflater mLayoutInflater;


    private OnItemClickListener mOnItemClickListener;


    public CallLogAdapter(Context context, OnItemClickListener listener) {
        this.mContext = context;
        mOnItemClickListener = listener;
        mLayoutInflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {

        return new NotificationViewHolder(mLayoutInflater.inflate(R.layout.layout_call_log_item, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {

        MergeCallLog callLog = mDatas.get(position);
        int type = callLog.getType();
        int callType = callLog.getCallType();
        final NotificationViewHolder holder = (NotificationViewHolder) viewHolder;
        final TextView itemTimeTv = holder.itemTypeTv;
        final TextView itemNameTv = holder.itemNameTv;
        String intervalTime = Utils.sessionTimeFormat(mContext,callLog.getTime());
        itemTimeTv.setText(intervalTime);
        itemNameTv.setText(callLog.getName());

        GlideApp.with(mContext)
                .load(callLog.getAvatarUrl())
                .placeholder((R.drawable.default_avatar))
                .error(R.drawable.default_avatar)
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .into(holder.itemAvatar);

        switch (callType) {
            case MergeCallLog.INCOMING_TYPE:
                Drawable drawable = null;
                if(type == MergeCallLog.CALL_TYPE){
                    drawable = ResourceManager.getInstance().getDrawable(R.drawable.call_in);
                } else if (type == MergeCallLog.VOICE_CALL_TYPE) {
//                    drawable = ResourceManager.getInstance().getDrawable(R.drawable.voice_call_in);
                }else if (type == MergeCallLog.VIDEO_CALL_TYPE) {
//                    drawable = ResourceManager.getInstance().getDrawable(R.drawable.viceo_call_in);
                }
                itemTimeTv.setCompoundDrawablesRelativeWithIntrinsicBounds(null, null, drawable, null);
                break;
            case MergeCallLog.OUTGOING_TYPE:
                Drawable callOutDrawable = null;
                if(type == MergeCallLog.CALL_TYPE){
                    callOutDrawable = ResourceManager.getInstance().getDrawable(R.drawable.call_out);
                } else if (type == MergeCallLog.VOICE_CALL_TYPE) {
//                    callOutDrawable = ResourceManager.getInstance().getDrawable(R.drawable.voice_call_out);
                }else if (type == MergeCallLog.VIDEO_CALL_TYPE) {
//                    callOutDrawable = ResourceManager.getInstance().getDrawable(R.drawable.video_call_out);
                }
                itemTimeTv.setCompoundDrawablesRelativeWithIntrinsicBounds(null, null, callOutDrawable, null);
                break;
            case MergeCallLog.MISSED_TYPE:
                Drawable missDrawable = null;
                if(type == MergeCallLog.CALL_TYPE){
                    missDrawable = ResourceManager.getInstance().getDrawable(R.drawable.call_miss);
                } else if (type == MergeCallLog.VOICE_CALL_TYPE) {
//                    missDrawable = ResourceManager.getInstance().getDrawable(R.drawable.voice_call_miss);
                }else if (type == MergeCallLog.VIDEO_CALL_TYPE) {
//                    missDrawable = ResourceManager.getInstance().getDrawable(R.drawable.video_call_miss);
                }
                itemTimeTv.setCompoundDrawablesRelativeWithIntrinsicBounds(null, null, missDrawable, null);
                break;
        }

        holder.itemLl.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                Log.e("TAG","=========onNoDoubleClick=======");
                mOnItemClickListener.onItemClick(position,callLog.getPhoneNumber());
            }
        });
    }

    @Override
    public int getItemCount() {
        final int size = mDatas.size();
        mOnItemClickListener.onRefreshEmptyView(size == 0);
        return size;
    }


    static class NotificationViewHolder extends RecyclerView.ViewHolder {

        RoundedImageView itemAvatar;
        TextView itemTypeTv;
        AppCompatTextView itemNameTv;
        RelativeLayout itemLl;

        public NotificationViewHolder(@NonNull View itemView) {
            super(itemView);
            itemAvatar = itemView.findViewById(R.id.avatar);
            itemTypeTv = itemView.findViewById(R.id.time_tv);
            itemNameTv = itemView.findViewById(R.id.name_tv);
            itemLl = itemView.findViewById(R.id.item_ll);
        }
    }


    public List<MergeCallLog> getDatas() {
        return mDatas;
    }

    public interface OnItemClickListener {
        void onItemClick(int position,String phoneNumber);

        void onRefreshEmptyView(boolean empty);
    }
}
